package com.lhr.common.search;

import java.io.IOException;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;
import com.lhr.common.util.ValidateUtil;


public class LuceneSearch {

	private LuceneSettings	settings;

	public LuceneSearch(LuceneSettings settings) {
		this.settings = settings;
	}

	public IndexSearcher createIndexSearcher() throws CorruptIndexException, IOException {
		String[] childFiles = settings.getDirectory().listAll();
		if (ValidateUtil.isEmpty(childFiles))
			return null;
		IndexSearcher searcher = new IndexSearcher(settings.getDirectory());
		searcher.setSimilarity(new IKSimilarity());
		return searcher;
	}

	public Query getQuery(String field, String keyword) throws IOException {
		return IKQueryParser.parse(field, keyword);
	}
}
